package com.cellulam.framework.core.tools;

import com.google.common.collect.Lists;
import org.apache.commons.lang3.RandomUtils;

import java.util.List;

public class RandomSelector<T> {

    private List<T> data = Lists.newArrayList();
    private final int weightScale;

    public RandomSelector() {
        this(1);
    }

    public RandomSelector(int weightScale) {
        this.weightScale = weightScale;
    }

    public void addSelector(double weight, T data) {
        if (weight <= 0) {
            return;
        }

        for (int i = 0; i < weight * weightScale; i++) {
            this.data.add(data);
        }
    }

    public void addSelector(int weight, T data) {
        addSelector((double) weight, data);
    }

    public T next() {
        return this.data.get(RandomUtils.nextInt(0, data.size()));
    }
}
